/*
 * Systemprogrammierung
 * Multiplayer-Quiz
 *
 * Server
 * 
 * clientthread.c: Implementierung des Client-Threads
 */

#include "common/rfc.h"
#include "user.h"
#include "login.h"
#include <stdbool.h>
#include <pthread.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include "catalog.h"

void *client_thread_main(int* client_id) {
	Question* shmQ;
	int QNumber;
	uint8_t correct;
	bool isGameActive = false;
	int id;
	int cSock;
	PACKET packet;
	packet.header.type = 0;
	packet.header.length = 0;
	PLAYER spieler;
	id = *client_id;
	infoPrint("Client_ID: %i", id);
	user_mutex_lock();
	spieler = getUser(id);
	user_mutex_unlock();
	cSock = spieler.sockDesc;
	while (1) {

		packet = packetLesen(cSock);
		infoPrint("Read: %i, Sender: %i", packet.header.type, id);
		if (packet.header.length != -1) {
			switch (packet.header.type) {
			case 0:
				infoPrint("Client: %i hat Spiel verlassen!", id);
				if (id == 0) {
					PACKET Error;
					Error.header.type = RFC_ERRORWARNING;
					Error.header.length = htons(
							strlen("Der Spielleiter hat das Spiel verlassen!"));
					Error.content.error.errortype = 1;
					strncpy(Error.content.error.errormessage,
							"Der Spielleiter hat das Spiel verlassen!",
							ntohs(Error.header.length));
					SendToAll(Error);
					infoPrint(
							"Der Server wurde geschlossen: Spielleiter hat das Spiel verlassen!");
					exit(0);
				}
				user_mutex_lock();
				removePlayer(id);
				user_mutex_unlock();
				sendPlayerList();
				pthread_exit(0);
				return NULL;
			case RFC_CATALOGREQUEST: // Catalog Request
				infoPrint("Catalog Request von: %i Name: %s", id,
						&spieler.name);
				user_mutex_lock();
				KatalogeSenden(cSock);
				if (isCatChosen()) {
					SendCatalogChange();
				}
				user_mutex_unlock();
				break;
			case RFC_CATALOGCHANGE:
				infoPrint("ID: %i", id);
				//Catalog Change
				setActiveCatalog(packet);
				SendCatalogChange();
				break;
			case RFC_STARTGAME: //Spielstart
				if (UserCount() < 2) {
					PACKET Error;
					Error.header.type = RFC_ERRORWARNING;
					Error.header.length = htons(strlen("Zuwenig Spieler!"));
					Error.content.error.errortype = 1;
					strncpy(Error.content.error.errormessage,
							"Zuwenig Spieler!", ntohs(Error.header.length));
					SendToAll(Error);
					infoPrint(
							"Zuwenig Spieler um das Spiel zu starten: Server wird geschlossen!");
					exit(0);
				} else {
					isGameActive = true;
					LoaderQuestions(packet.content.catalogname);
					QNumber = 0;
					SendToAll(packet);
				}
				break;
			case RFC_QUESTIONREQUEST: //QuestionRequest
			{
				shmQ = getQuestion(QNumber);
				QNumber++;
				correct = shmQ->correct;
				PACKET question;
				question.header.type = RFC_QUESTION;
				question.header.length = htons(sizeof(QuestionMessage));
				QuestionMessage quest;
				strcpy(quest.question, shmQ->question);
				strcpy(quest.answers[0], shmQ->answers[0]);
				strcpy(quest.answers[1], shmQ->answers[1]);
				strcpy(quest.answers[2], shmQ->answers[2]);
				strcpy(quest.answers[3], shmQ->answers[3]);
				quest.timeout = shmQ->timeout;
				question.content.question = quest;
				packetSchreiben(question, cSock);
				sendPlayerList();
				break;
			}
			case RFC_QUESTIONANSWERED: {
				PACKET QAnswer;
				QAnswer.header.type = RFC_QUESTIONRESULT;
				QAnswer.header.length = htons(2);
				QAnswer.content.antwort.correct = correct;
				QAnswer.content.antwort.TimedOut = 0;
				packetSchreiben(QAnswer, cSock);
				break;
			}
			default:
				infoPrint("Empfange Message. Type: %i", packet.header.type);
				break;
			}

		}
	}
	pthread_exit(0);
	return NULL;
}
